package edu.hawaii.halealohacli.jcev.command;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import edu.hawaii.halealohacli.jcev.processor.ExitException;

/**
 * Tests the functionality Quit class which implements the Command interface.
 * @author Christopher Foo
 *
 */
public class TestQuit {
  
  /**
   * A Quit object to perform the tests on.
   */
  Quit quit = new Quit();
  
  /**
   * Tests the verifyArguments method of the Quit class which returns true if the string
   * passed represents valid arguments for the quit command or false if the string is invalid.
   */
  @Test
  public void testVerifyArguments() {
    
    //Valid Arguments
    assertTrue("Empty string is a valid argument, but returned false.", quit.verifyArguments(""));
    assertTrue("Whitespace is a valid argument, but returned false.", quit.verifyArguments("    "));
    
    //Invalid Arguments
    assertFalse("Non-empty string is an invalid argument, but returned true.", 
                 quit.verifyArguments("notempty"));
    assertFalse("Non-empty string with whitespace is an invalid argument, but returned true.", 
                 quit.verifyArguments("  notempty "));
  }
  
  /**
   * Tests that the execute method will throw the ExitException used to quit the CLI when
   * passed valid arguments.
   */
  @Test
  public void testExit() {
    boolean successEmptyString = false;
    boolean successWhitespace = false;
    
    //Test empty string
    try {
      quit.execute("", null);
    }
    catch (ExitException e) {
      successEmptyString = true;
    }
    catch (Exception e) {
      fail("Failure: Exception during valid execution.");
    }
    
    //Test whitespace
    try {
      quit.execute("     ", null);
    }
    catch (ExitException e) {
      successWhitespace = true;
    }
    catch (Exception e) {
      fail("Failure: Exception during valid execution.");
    }

    assertTrue("Failed to quit with empty string.", successEmptyString);
    assertTrue("Failed to quit with only whitespace.", successWhitespace);
  }

  /**
   * Tests that the execute method throws a generic exception (not an ExitException) when passed
   * invalid arguments.
   */
  @Test
  public void testInvalidArgument() {
    boolean caught = false;
    try {
      quit.execute("notempty", null);
    }
    catch (ExitException e) {
      fail("Failure: ExitException during invalid execution.");
    }
    catch (Exception e) {
      caught = true;
    }
    assertTrue("Invalid Argument did not throw Exception.", caught);
  }
}
